package com.haruhi.bot.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;

/**
 * http请求上传文件限制文件大小
 * springboot maxFileSize默认为1MB
 */
@Configuration
public class UploadFileSizeConfig {

    @Value("${fileMaxSize}")
    private String maxSize;
    @Value("${requestTotalSize}")
    private String requestTotalSize;

    @Bean
    public MultipartConfigElement multipartConfigElement(){
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 设置上传允许的文件大小
        factory.setMaxFileSize(DataSize.parse(maxSize));
        // 设置上传允许的请求总大小
        factory.setMaxRequestSize(DataSize.parse(requestTotalSize));
        return factory.createMultipartConfig();
    }

}
